package com.zgz.underwrite.util;

import ilog.rules.res.model.IlrPath;
import ilog.rules.res.session.IlrEJB3SessionFactory;
import ilog.rules.res.session.IlrSessionRequest;
import ilog.rules.res.session.IlrSessionResponse;
import ilog.rules.res.session.IlrStatelessSession;
import ilog.rules.res.session.ruleset.IlrBusinessExecutionTrace;
import ilog.rules.res.session.ruleset.IlrExecutionTrace;

import java.util.List;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * 
 * @author zgz
 *
 */
public class ILogUtil {
//	public static final String PROVIDER_URL = IrlConfig.getProperty("PROVIDER_URL");
//	public static final String INITIAL_CONTEXT_FACTORY = IrlConfig.getProperty("INITIAL_CONTEXT_FACTORY");
//	public static final String REMOTE_NAME = IrlConfig.getProperty("REMOTE_NAME");
//	public static final String RULESET_PATH = IrlConfig.getProperty("RULESET_PATH");
	
	public static final String PROVIDER_URL = "ejbd://127.0.0.1:4201";
	public static final String INITIAL_CONTEXT_FACTORY = "org.apache.openejb.client.RemoteInitialContextFactory";
	public static final String REMOTE_NAME = "IlrStatelessSessionEJB3Remote";
	public static final String RULESET_PATH = "/underwriteruleapp/1.0/underwriterules/1.0";
	
	public final static IlrEJB3SessionFactory ilrFactory = new IlrEJB3SessionFactory();
	public static IlrStatelessSession ilrStatelessSession;
	
	public static IlrStatelessSession getIlrStatelessSession() throws Exception {
		if (ilrStatelessSession != null) {
			return ilrStatelessSession;
		}
		Properties props = new Properties();
		props.setProperty(Context.PROVIDER_URL, PROVIDER_URL);
		props.setProperty(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
		InitialContext context = new InitialContext(props);
		ilrStatelessSession = (IlrStatelessSession)context.lookup(REMOTE_NAME);
		return ilrStatelessSession;
	}


	public static IlrSessionRequest getDefltIlrSessionRequest() throws Exception {
		IlrSessionRequest sessionReq = ilrFactory.createRequest();
		sessionReq.setRulesetPath(IlrPath.parsePath(RULESET_PATH));
		sessionReq.setForceUptodate(true);
		sessionReq.setTraceEnabled(true);
		sessionReq.getTraceFilter().setInfoAllFilters(true);
		return sessionReq;
	}
	
	public static String getExecTraceReslt(IlrSessionResponse sessionResponse) throws Exception {
		IlrExecutionTrace trace = sessionResponse.getRulesetExecutionTrace();
		IlrBusinessExecutionTrace execResult = new IlrBusinessExecutionTrace(trace);
		StringBuilder sbTrace = new StringBuilder();
		if (trace != null) {
			sbTrace.append(">>>>>>>>>>>>总共有" + trace.getTotalRulesFired() + "条规则被执行：\n");
			List<String> rules = execResult.getRuleFiredBusinessNames();
			if (rules!=null) {
				for (int i = 0; i < rules.size (); i++) {
					sbTrace.append(">>>>>>>>>>>>第 "+(i+1)+" 条" + ": " + rules.get(i)+"\n");
				}
			}
		} else {
			sbTrace.append(">>>>>>>>>>>>没有符合条件的规则被执行!");
		}
		return sbTrace.toString();
	}
}
